<template>
	<view class="content">
		<view class="uni-divider uni-divider__content">RDB_Demo</view>
		<button class="button" @click="createTable">创建表, 结果:{{createTableResult}}</button>
		<button class="button" @click="insertTableData">新增表数据, 结果:{{insertTableDataResult}} 自增id：{{data_id}}</button>
		<button class="button" @click="deleteTableData">删除表数据, 结果:{{deleteTableDataResult}} </button>
		<button class="button" @click="deleteTable">删除表, 结果:{{deleteTableResult}} </button>
		<button class="button" @click="selectTableData">查询表数据, 结果:{{listData}}</button>
	</view>
</template>

<script>
	import {
		createTable,
		insert
	} from '@/uni_modules/test-relationalStore'

	export default {
		data() {
			return {
				createTableResult: '',
				insertTableDataResult: '',
				updateTableDataResult: '',
				deleteTableDataResult: '',
				deleteTableResult: '',
				listData: [],
				data_id: 1
			}
		},

		methods: {
			createTable() {
				let columnList = [{
						'name': 'id',
						'type': 'integer',
						'length': -1,
						'nullable': false,
						'primary': true,
						'autoincrement': true
					},
					{
						'name': 'name',
						'type': 'text',
						'length': -1,
						'nullable': true,
						'primary': false,
						'autoincrement': false
					},
					{
						'name': 'status',
						'type': 'text',
						'length': -1,
						'nullable': true,
						'primary': false,
						'autoincrement': false
					}
				];

				uni.createTable('demo_test', columnList)
					.then(() => {
						console.log('createTable success: ');
						this.createTableResult = 'success';
					})
					.catch((err) => {
						console.error('createTable fail: ', JSON.stringify(err));
						this.createTableResult = 'fail';
					})

			},

			insertTableData() {
				let taskData = {
					'id': this.data_id,
					'name': 'test',
					'status': 'on'
				}

				uni.insert('demo_test', taskData)
					.then(() => {
						console.log('insertTableData success: ');
						this.insertTableDataResult = 'success';
						this.data_id++
					})
					.catch((err) => {
						console.error('insertTableData fail: ', JSON.stringify(err));
						this.insertTableDataResult = 'fail';
					})
			},

			selectTableData() {
				uni.queryAll('demo_test')
					.then(list => {
						console.log('selectTableData success: ' + JSON.stringify(list));
						this.listData = list
					})
					.catch((err) => {
						console.error('selectTableData fail: ', JSON.stringify(err));
					})
			},

			deleteTableData() {
				let sql = 'delete from demo_test'
				uni.executeSql(sql)
					.then(() => {
						console.log('deleteTableData success: ');
						this.deleteTableDataResult = 'success';

					})
					.catch((err) => {
						console.error('deleteTableData fail: ', JSON.stringify(err));
						this.deleteTableDataResult = 'fail';
					})
			},

			deleteTable() {
				uni.deleteTable('demo_test')
					.then(() => {
						console.log('deleteTable success: ');
						this.deleteTableResult = 'success';

					})
					.catch((err) => {
						console.error('deleteTable fail: ', JSON.stringify(err));
						this.deleteTableResult = 'fail';
					})
			}
		},
		onShow() {
			this.gd = getApp().globalData
		}
	}
</script>

<style>
	.button {
		margin: 30rpx;
		color: #007AFF;
	}

	.text {
		margin-left: 30rpx;
	}
</style>